package database;
import database.dbtable.MySqlClient;
import database.dbtable.MySqlClients;
import logger.LoggerManager;
import parsers.UserDataParser;
import resurses.LoggerMessage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by alexander.makalov on 22.10.2014.
 */
public class DatabaseCreator {

    public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private String DB_URL;

    private String USER;
    private String PASS;

    public DatabaseCreator() {
        if (UserData.checkData()){
            DB_URL = UserData.getDbUrl();
            USER = UserData.getUserName();
            PASS = UserData.getUserPass();
        }
    }

    public DatabaseCreator(String db_url, String user, String pass) {
        DB_URL = db_url;
        USER = user;
        PASS = pass;
    }

    public void createORCheckDatabaseInfo() {
        Connection conn = null;
        Statement stmt = null;
        try {

            Class.forName(JDBC_DRIVER);

            LoggerManager.getLogger().Log(LoggerMessage.CONNECT_TO_DATABASE, LoggerManager.LogLevel.DEBUG);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            LoggerManager.getLogger().Log(LoggerMessage.CHECK_DATABASE, LoggerManager.LogLevel.DEBUG);
            stmt = conn.createStatement();

            String sql = MySqlClients.CREATE_DB_QUERY_CLIENTS;
            stmt.executeUpdate(sql);
            LoggerManager.getLogger().Log(LoggerMessage.CHECK_DATABASE_FINISH_OK, LoggerManager.LogLevel.DEBUG);

            Connection conn2 = conn = DriverManager.getConnection(DB_URL+ MySqlClients.CLIENTS_TABLE_NAME, USER, PASS);
            stmt = null;
            stmt = conn2.createStatement();
            LoggerManager.getLogger().Log(String.format(LoggerMessage.CHECK_TABLE, MySqlClient.CLIENT_TABLE_NAME), LoggerManager.LogLevel.DEBUG);
            String sql_client_query = MySqlClient.CREATE_TABLE_QUERY_CLIENT;

            stmt.executeUpdate(sql_client_query);
            LoggerManager.getLogger().Log(LoggerMessage.CHECK_TABLE_OK, LoggerManager.LogLevel.DEBUG);
        } catch (SQLException se) {
            LoggerManager.getLogger().Log(se.getMessage(), LoggerManager.LogLevel.ERROR);
            //se.printStackTrace();
        } catch (Exception e) {
            LoggerManager.getLogger().Log(e.getMessage(), LoggerManager.LogLevel.ERROR);
            //e.printStackTrace();
        } finally {

            try {
                if (stmt != null){
                    stmt.close();
                }
            } catch (SQLException se) {
                LoggerManager.getLogger().Log(se.getMessage(), LoggerManager.LogLevel.ERROR);
                //se.printStackTrace();
            }
            try {
                if (conn != null){
                    conn.close();
                }
            } catch (SQLException se) {
                LoggerManager.getLogger().Log(se.getMessage(), LoggerManager.LogLevel.ERROR);
                //se.printStackTrace();
            }
        }
        LoggerManager.getLogger().Log(LoggerMessage.HAVE_A_NICE_DAY, LoggerManager.LogLevel.DEBUG);
    }
}
